package hu.plusone.eregister.controller;

import static hu.plusone.eregister.controller.AbstractController.PAGE_SIZE;
import hu.plusone.eregister.entity.Lesson;
import hu.plusone.eregister.service.AppService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LessonController extends AbstractController {

    @Autowired
    public LessonController(AppService service) {
        super(service);
    }

    @RequestMapping(value = "/savelesson")
    public void saveLesson(@RequestBody Lesson lesson) {
        service.saveLesson(lesson);
    }

    @RequestMapping(value = "/deletelesson")
    public void deleteLesson(@RequestBody Lesson lesson) {
        service.deleteLesson(lesson);
    }

    @RequestMapping(value = {"/diary/{diary}/{page}", "/diary/{diary}"})
    public @ResponseBody
    List<Lesson> findAllByDiary(@PathVariable("diary") String diaryId, @PathVariable(value = "page") Integer page) {
        page = normalizePage(page);
        return service.getLessons(diaryId, PAGE_SIZE, page);
    }
}
